/**
 * @作者 伟子涵
 * @微信公号 搬砖暗夜码农
 * @开源项目 $ https://gitee.com/zhouwei5208888
 * @我的博客 $ http://itzhouwei.com
 * @创建时间 2023/9/12 9:43
 */
package com.itzhouwei.commerce.controller;

import com.gearwenxin.client.ernie.ErnieBotTurboClient;
import com.gearwenxin.entity.chatmodel.ChatBaseRequest;
import com.gearwenxin.entity.response.ChatResponse;
import com.itzhouwei.commerce.vo.CommonResponse;
import jakarta.annotation.Resource;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
import reactor.core.publisher.Flux;

import java.io.IOException;


/**
 * @description:
 * @author：zw，微信：yingshengzw
 * @date: 2023/9/12
 * @Copyright： 公众号：搬砖暗夜码农 | 博客：https://itzhouwei.com - 沉淀、分享、成长，让自己和他人都能有所收获！
 */
@RestController
@RequestMapping("/turbo")
public class ErnieBotTurboController {

    @Resource
    private ErnieBotTurboClient ernieBotTurboClient;

    //------------------默认参数------------------//

    // 单轮对话
    @PostMapping("/chat")
    public ChatResponse chatSingle(String msg) {
        ChatResponse chatResponse = ernieBotTurboClient.chatSingle(msg).block();
        return chatResponse;
    }

    // 连续对话
    @PostMapping("/chats")
    public ChatResponse chatCont(String msg, String msgUid) {
        ChatResponse response = ernieBotTurboClient.chatCont(msg, msgUid).block();
        String result = response.getResult();
        if (result.contains("百度")) {
            result = result.replaceAll("百度", "车卫士");
        }
        if (result.contains("文心一言")){
            result=result.replaceAll("文心一言","小码哥");
        }
        if (result.contains("英文名是ERNIE Bot")){
            result=result.replaceAll("英文名是ERNIE Bot","");
        }
        response.setResult(result);
        return response;
    }

    // 流式返回，单次对话
    @PostMapping(value = "/stream/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> chatSingleStream(String msg) {
        return ernieBotTurboClient.chatSingleOfStream(msg);
    }

    // 流式返回，连续对话
    @PostMapping(value = "/stream/chats", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public SseEmitter chatContStream(String msg, String msgUid) {
        //设置默认的超时时间60秒
        final SseEmitter emitter = new SseEmitter(60 * 1000L);
        try {
            Flux<ChatResponse> response = ernieBotTurboClient.chatContOfStream(msg, msgUid);

            response.subscribe(chatResponse -> {
                try {
                    if(chatResponse.getIsEnd()){
                        emitter.complete();
                    }else {
                        emitter.send(chatResponse.getResult());
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            });
        }catch (Exception e){
            emitter.completeWithError(e);
        }
        return emitter;
    }

    //------------------自定义参数------------------//

    // 模板对话
    @PostMapping("/param/chat")
    public ChatResponse pChatSingle(@RequestBody ChatBaseRequest chatBaseRequest) {
        ChatResponse chatResponse = ernieBotTurboClient.chatSingle(chatBaseRequest).block();
        return chatResponse;
    }

    // 连续对话
    @PostMapping("/param/chats")
    public ChatResponse pChatCont(@RequestBody ChatBaseRequest chatBaseRequest) {
        ChatResponse response = ernieBotTurboClient.chatCont(chatBaseRequest, chatBaseRequest.getUserId()).block();
        return response;
    }

    // 流式返回，单次对话
    @PostMapping(value = "/param/stream/chat", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> pChatSingleStream(@RequestBody ChatBaseRequest chatBaseRequest) {
        return ernieBotTurboClient.chatSingleOfStream(chatBaseRequest);
    }

    // 流式返回，连续对话
    @PostMapping(value = "/param/stream/chats", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public Flux<ChatResponse> pChatContStream(@RequestBody ChatBaseRequest chatBaseRequest, String msgUid) {
        return ernieBotTurboClient.chatContOfStream(chatBaseRequest, msgUid);
    }
}
